// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI);
import axios from 'axios'

import echarts from 'echarts'




Vue.prototype.$echarts = echarts //注册组件


Vue.config.productionTip = false

axios.defaults.baseURL = '/api'
let http = axios.create({})
http.interceptors.response.use(
    response =>{
        // 判断
        if(response.data.code == 403){
            // 后台返回没登录
            // 跳转到登录页（路由）
            //window.location.href = "/login"
            if(vue.$route.path == '/login'){
                return response
            }else{
                vue.$router.push("/login")
                return
            }
        }

        // 判断返回的是否是没权限，是则进行提示
        if(response.data.code == 401){
            vue.$message.error(response.data.message)
            return
        }

        return response
    }
)
http.interceptors.request.use(
    config => {
        // 得到token  本地
            let basic = "Basic " + btoa("woniuxy:123","utf8");
            let token  = window.localStorage.getItem("access_token")
            let refreshtoken = window.localStorage.getItem("refresh_token")
        if (!config.url.startsWith("oauth/token")) {
            config.headers["Authorization"] = "Bearer " +token
            config.headers["refreshtoken"] = refreshtoken
        }else {
            config.headers["Authorization"] = basic
        }
        // 放行请求
        return config
    }
)


// 将axios设置成vue的属性
Vue.prototype.$axios = http

Vue.config.productionTip = false


/* eslint-disable no-new */
new Vue({
    el: '#app',
    router,
    components: {App},
    template: '<App/>'
})
